package com.elitedatai.mchtest.mission.controller;

import com.dji.sdk.common.HttpResultResponse;
import com.elitedatai.mchtest.mission.model.dto.FlightMissionDTO;
import com.elitedatai.mchtest.mission.service.IFlightMissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 飞行任务控制器
 * 提供飞行任务的增删改查REST API接口
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/flight-missions")
public class FlightMissionController {

    @Autowired
    private IFlightMissionService flightMissionService;

    /**
     * 创建飞行任务
     * @param flightMissionDTO 飞行任务数据传输对象
     * @return 创建的飞行任务信息
     */
    @PostMapping
    public HttpResultResponse<FlightMissionDTO> createFlightMission(@Valid @RequestBody FlightMissionDTO flightMissionDTO) {
        try {
            FlightMissionDTO createdMission = flightMissionService.createFlightMission(flightMissionDTO);
            return HttpResultResponse.success(createdMission);
        } catch (Exception e) {
            log.error("创建飞行任务失败", e);
            return HttpResultResponse.error("创建飞行任务失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取飞行任务
     * @param id 飞行任务ID
     * @return 飞行任务信息
     */
    @GetMapping("/{id}")
    public HttpResultResponse<FlightMissionDTO> getFlightMissionById(@PathVariable Long id) {
        try {
            FlightMissionDTO flightMission = flightMissionService.getFlightMissionById(id).orElse(null);
            if (flightMission != null) {
                return HttpResultResponse.success(flightMission);
            } else {
                return HttpResultResponse.error("飞行任务不存在");
            }
        } catch (Exception e) {
            log.error("获取飞行任务失败", e);
            return HttpResultResponse.error("获取飞行任务失败: " + e.getMessage());
        }
    }

    /**
     * 根据任务ID获取飞行任务
     * @param taskId 任务ID
     * @return 飞行任务信息
     */
    @GetMapping("/task/{taskId}")
    public HttpResultResponse<FlightMissionDTO> getFlightMissionByMissionId(@PathVariable String taskId) {
        try {
            FlightMissionDTO flightMission = flightMissionService.getFlightMissionByMissionId(taskId).orElse(null);
            if (flightMission != null) {
                return HttpResultResponse.success(flightMission);
            } else {
                return HttpResultResponse.error("飞行任务不存在");
            }
        } catch (Exception e) {
            log.error("获取飞行任务失败", e);
            return HttpResultResponse.error("获取飞行任务失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有飞行任务
     * @return 所有飞行任务列表
     */
    @GetMapping
    public HttpResultResponse<List<FlightMissionDTO>> getAllFlightMissions() {
        try {
            List<FlightMissionDTO> flightMissions = flightMissionService.getAllFlightMissions();
            return HttpResultResponse.success(flightMissions);
        } catch (Exception e) {
            log.error("获取飞行任务列表失败", e);
            return HttpResultResponse.error("获取飞行任务列表失败: " + e.getMessage());
        }
    }

    /**
     * 分页获取飞行任务
     * @param page 页码（从0开始）
     * @param size 每页大小
     * @return 分页的飞行任务列表
     */
    @GetMapping("/page")
    public HttpResultResponse<Page<FlightMissionDTO>> getFlightMissionsPage(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Pageable pageable = PageRequest.of(page, size);
            Page<FlightMissionDTO> flightMissionsPage = flightMissionService.getFlightMissionsPage(pageable);
            return HttpResultResponse.success(flightMissionsPage);
        } catch (Exception e) {
            log.error("分页获取飞行任务失败", e);
            return HttpResultResponse.error("分页获取飞行任务失败: " + e.getMessage());
        }
    }

    /**
     * 更新飞行任务
     * @param id 飞行任务ID
     * @param flightMissionDTO 更新的飞行任务数据
     * @return 更新后的飞行任务信息
     */
    @PutMapping("/{id}")
    public HttpResultResponse<FlightMissionDTO> updateFlightMission(@PathVariable Long id,
                                                                 @Valid @RequestBody FlightMissionDTO flightMissionDTO) {
        try {
            flightMissionDTO.setId(id);
            FlightMissionDTO updatedMission = flightMissionService.updateFlightMission(flightMissionDTO);
            if (updatedMission != null) {
                return HttpResultResponse.success(updatedMission);
            } else {
                return HttpResultResponse.error("飞行任务不存在");
            }
        } catch (Exception e) {
            log.error("更新飞行任务失败", e);
            return HttpResultResponse.error("更新飞行任务失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID删除飞行任务
     * @param id 飞行任务ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public HttpResultResponse<Void> deleteFlightMissionById(@PathVariable Long id) {
        try {
            boolean deleted = flightMissionService.deleteFlightMissionById(id);
            if (deleted) {
                return HttpResultResponse.success();
            } else {
                return HttpResultResponse.error("飞行任务不存在");
            }
        } catch (Exception e) {
            log.error("删除飞行任务失败", e);
            return HttpResultResponse.error("删除飞行任务失败: " + e.getMessage());
        }
    }

    /**
     * 根据任务ID删除飞行任务
     * @param taskId 任务ID
     * @return 删除结果
     */
    @DeleteMapping("/task/{taskId}")
    public HttpResultResponse<Void> deleteFlightMissionByMissionId(@PathVariable String taskId) {
        try {
            boolean deleted = flightMissionService.deleteFlightMissionByMissionId(taskId);
            if (deleted) {
                return HttpResultResponse.success();
            } else {
                return HttpResultResponse.error("飞行任务不存在");
            }
        } catch (Exception e) {
            log.error("删除飞行任务失败", e);
            return HttpResultResponse.error("删除飞行任务失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除飞行任务
     * @param ids 飞行任务ID列表
     * @return 删除结果
     */
    @DeleteMapping("/batch")
    public HttpResultResponse<Void> deleteFlightMissionsByIds(@RequestBody List<Long> ids) {
        try {
            flightMissionService.deleteFlightMissionsByIds(ids);
            return HttpResultResponse.success();
        } catch (Exception e) {
            log.error("批量删除飞行任务失败", e);
            return HttpResultResponse.error("批量删除飞行任务失败: " + e.getMessage());
        }
    }
}